//
//  UIControl+acceptEvent.m
//  AGG_V2
//
//  Created by admin on 15/10/27.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "UIControl+acceptEvent.h"
#import "AppDelegate.h"

static const char *Agg_acceptEventInterval = "Agg_acceptEventInterval";
static const char *Agg_ignoreEvent         = "Agg_ignoreEvent";
static const char *Agg_acceptEvent         = "Agg_acceptEvent";

@implementation UIControl (acceptEvent)

@dynamic agg_acceptEventInterval, agg_ignoreEvent;

+ (void)load {
    Method originalMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method swizzledMethod = class_getInstanceMethod(self, @selector(__agg_sendAction:to:forEvent:));
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (NSTimeInterval)agg_acceptEventInterval {
  return [objc_getAssociatedObject(self, Agg_acceptEventInterval) doubleValue];
}

- (void)setAgg_acceptEventInterval:(NSTimeInterval)agg_acceptEventInterval {
  objc_setAssociatedObject(self, Agg_acceptEventInterval,
                           @(agg_acceptEventInterval),
                           OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)agg_ignoreEvent {
  return [objc_getAssociatedObject(self, Agg_ignoreEvent) boolValue];
}

- (void)setAgg_ignoreEvent:(BOOL)agg_ignoreEvent {
  objc_setAssociatedObject(self, Agg_ignoreEvent, @(agg_ignoreEvent),
                           OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)agg_acceptEvent {
    return [objc_getAssociatedObject(self, Agg_acceptEvent) boolValue];
}

- (void)setAgg_acceptEvent:(BOOL)agg_acceptEvent {
    objc_setAssociatedObject(self, Agg_acceptEvent, @(agg_acceptEvent),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)__agg_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if (self.agg_acceptEvent) {
        [self __agg_sendAction:action to:target forEvent:event];
        return;
    }
    //招行键盘特别处理
    Class class = NSClassFromString(@"SKeyboardNumbersView");
    if ([target isKindOfClass:class]) {
        [self __agg_sendAction:action to:target forEvent:event];
        return;
    }
    //NSLog(@"target--%@--%@--%@",target,event,[self class]);
    // CMKShutterButton为相机拍照按钮
    if (!([NSStringFromClass([self class]) isEqualToString:@"UITabBarButton"]
          || [NSStringFromClass([self class]) isEqualToString:@"RCTTextField"]
          || [NSStringFromClass([self class]) isEqualToString:@"CAMShutterButton"]
          || [NSStringFromClass([self class]) isEqualToString:@"CMKShutterButton"]
          || [NSStringFromClass([self class]) isEqualToString:@"CUShutterButton"])) {
           //NSLog(@"0000");
           //NSLog(@"target11--%@--%@--%@",target,event,[self class]);
        AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
        if (appDelegate.clickControl && ![appDelegate.clickControl isEqual:self]) {
            appDelegate.clickControl.agg_ignoreEvent = NO;
        }
        appDelegate.clickControl = self;
        if (self.agg_ignoreEvent) {
            return;
        }
        if (self.agg_acceptEventInterval == 0) {
            self.agg_acceptEventInterval = 2;
        }
        if (self.agg_acceptEventInterval > 0) {
            self.agg_ignoreEvent = YES;
            
            [self performSelector:@selector(__setAggIgnoreEvent:)
                       withObject:@(NO)
                       afterDelay:self.agg_acceptEventInterval];
        }
    } else {
        //NSLog(@"1111");
    }
    //NSLog(@"2222");
    [self __agg_sendAction:action to:target forEvent:event];
}

- (void)__setAggIgnoreEvent:(NSNumber *)agg_ignoreEvent {
    BOOL ignoreEvent = [agg_ignoreEvent boolValue];
    self.agg_ignoreEvent = ignoreEvent;
}


@end
